#include"../mqserver/mq_consumer.hpp"
#include<gtest/gtest.h>

xiang11mq::ConsumerManager::ptr cmp;

class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        std::cout << "单元测试执行前的环境初始化" << std::endl;
        cmp = std::make_shared<xiang11mq::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
    }
    virtual void TearDown() override
    {
        std::cout << "单元测试执行完毕后的环境清理" << std::endl;
        cmp->clear();
    }
};

void cb(const std::string tag, xiang11mq::BasicProperties *bp, const std::string body)
{
    std::cout << tag << "消费了 消息" << body << std::endl;
}

TEST(ConsumerTest, insert_test)
{
    cmp->create("queue1", "consumer1", false, cb);
    cmp->create("queue1", "consumer2", false, cb);
    cmp->create("queue1", "consumer3", false, cb);

    ASSERT_EQ(cmp->exists("queue1", "consumer1"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer3"), true);
}

TEST(ConsumerTest, remove_test)
{
    cmp->remove("queue1", "consumer1");
    ASSERT_EQ(cmp->exists("queue1", "consumer1"), false);
    ASSERT_EQ(cmp->exists("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exists("queue1", "consumer3"), true);
}
TEST(ConsumerTest, choose_test) 
{
    xiang11mq::Consumer::ptr cp = cmp->choose("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag, "consumer2");

    cp = cmp->choose("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag, "consumer3");

    cp = cmp->choose("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->tag, "consumer2");
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);

    testing::AddGlobalTestEnvironment(new ConsumerTest);
    //testing::AddGlobalTestEnvironment(new MyMapTest);
    int ret = RUN_ALL_TESTS();
    return 0;
}


